# ma15mm8k24k.py 文件内容示例
import sqlite3
import re
import numpy as np
def helloworld():
    print("Hello World!",__name__ )

def query_sqlite_database(path,DB_TABLE_NAME):
    print("查询SQLite数据库")
    try:
        # 连接到SQLite数据库
        conn = sqlite3.connect(path)
        cursor = conn.cursor()
        # 执行查询语句
        cursor.execute("SELECT * FROM " + DB_TABLE_NAME)
        results = cursor.fetchall()
        processed_results = []
        for row in results:
            # 提取时间信息
            time_str = re.search(r"(\d{4}/\d{2}/\d{2}-\d{2}:\d{2})", row[0]).group(1) if re.search(r"(\d{4}/\d{2}/\d{2}-\d{2}:\d{2})", row[0]) else ""
             # 将 row[1] 转换为字符串
           
            # 提取预测K线信息
            predict_kline_match = re.search(r"(预测K线(与优良度)?):.*?array\(\[(.*?)\]\)", str(row[1])) or re.search(r"(预测K线):.*?array\(\[(.*?)\]\)", str(row[1]))
            if predict_kline_match:
                predict_kline_str = predict_kline_match.group(3)
                predict_kline = np.fromstring(predict_kline_str.replace('[', '').replace(']', ''), sep=',')
                predict_kline = predict_kline.astype(int)  # 将预测K线数据转换为整数类型
            else:
                # 处理未匹配到的情况
                predict_kline = np.array([])
              # 将 row[1] 转换为字符串
            row_1_str = str(row[1])
            # 提取收盘价信息，直接提取末尾数字
            close_price_str = ''.join(filter(str.isdigit, row_1_str.split()[-1]))
            if close_price_str:
                close_price = int(close_price_str)
                #print(f"匹配到收盘价: {close_price}")
            else:
                #print(f"未提取到收盘价，原始数据: {row_1_str}")
                close_price = 0
            if len(predict_kline) > 0:
                new_data_list = predict_kline + close_price
                predict_kline = np.vstack((predict_kline, new_data_list))

            processed_results.append([time_str, predict_kline.tolist(), close_price])

        cursor.close()
        conn.close()
        return processed_results
    except sqlite3.Error as e:
        return f"数据库操作出错: {e}"




